package org.advanced.pool;

import java.util.concurrent.*;

/**
 * @author John.zhang
 * @version Id: CallableFutures, v 1.0 2020/12/11 16:39 ZhiYong Exp $
 * 带返回值的线程
 * Callable 函数式接口，指定接口只有一个抽象方法，可以使用lama表达式
 */
public class CallableFutures implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println("Hello John");
        Thread.sleep(3000);
        return "John";
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //默认方式
        CallableFutures callableFutures = new CallableFutures();
        FutureTask futureTask = new FutureTask(callableFutures);
        new Thread(futureTask).start();
        System.out.println(futureTask.get());

        //线程池的方式
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        CallableFutures callableFutures2 = new CallableFutures();
        FutureTask submit = (FutureTask) executorService.submit(callableFutures2);
        System.out.println(submit.get());
    }

}
